package controller;

/**
 * The LoginController provides all the methods needed to update the LoginDisplay class
 * and receive input from it. It acts as an intermediate between the login GUI and the server.
 */

import gui.Display;
import gui.LoginDisplay;

public class LoginController extends Controller {
	/**
	 * Constructor method.
	 */
	public LoginController() {
		setDisplay(new LoginDisplay(this));
	}
	
	/**
	 * Constructor method. Sets the Display according to the previous display.
	 * @param display The previous Display.
	 */
	public LoginController(Display display) {
		LoginDisplay loginDisplay = new LoginDisplay(this, display);
		setDisplay(loginDisplay);
	}
	
	/**
	 * Connects with the server to verify the login, then starts the LobbyController and LobbyDisplay.
	 * @param username Username entered at login
	 * @param password Password entered at login
	 */
	public void login(String username, String password) {
		try {
			
			user = serverRemote.login(username, password);
			
			if(user != null)  {
				Controller lobbyController = new LobbyController(display);
				this.setUser(user);
				this.giveControlTo(lobbyController);
			}
			else {
				
			}
		}
		catch(Exception e) {
			
		}
	}
	
	/**
	 * Switch to the registration screen by giving control to the
	 * registration controller.
	 */
	public void register() {
		Controller registrationController = new RegistrationController(display);
		this.giveControlTo(registrationController);
	}
	
}
